package com.xyz.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.NumberUtil;
import cn.hutool.core.util.StrUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.xyz.dao.mapper.BusinessFileDaoMapper;
import com.xyz.dto.SelectPageAllDto;
import com.xyz.entity.mapper.*;
import com.xyz.entity.pojo.EquipmentTree;
import com.xyz.entity.pojo.EquipmentTreeExample;
import com.xyz.entity.pojo.RepairWorkOrder;
import com.xyz.service.CommonService;
import com.xyz.service.RepairAppService;
import com.xyz.util.dto.DataResult;
import com.xyz.util.dto.GridData;
import com.xyz.util.dto.LoginInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Transactional(rollbackFor = Exception.class)
public class RepairAppServiceImpl implements RepairAppService {

    @Autowired
    private CommonService commonService;

    @Autowired
    private RepairWorkOrderMapper repairWorkOrderMapper;

    @Autowired
    private RepairMaintenanceMapper repairMaintenanceMapper;

    @Autowired
    private RepairMaintenanceSuspendMapper repairMaintenanceSuspendMapper;

    @Autowired
    private BusinessAnnounceMapper businessAnnounceMapper;

    @Autowired
    private BusinessAcceptMapper businessAcceptMapper;

    @Autowired
    private BusinessFileMapper businessFileMapper;

    @Autowired
    private EquipmentInfoMapper equipmentInfoMapper;

    @Autowired
    private RepairWorkOrderFlowMapper repairWorkOrderFlowMapper;

    @Autowired
    private BusinessFileDaoMapper businessFileDao;


    /*
    * App待审核查询
    * */
    @Override
    public DataResult getPendingRepairAll(LoginInfo loginInfo, SelectPageAllDto selectPageAllDto) throws Exception {
        Page<RepairWorkOrder> pageObject = PageHelper.startPage(NumberUtil.parseInt(selectPageAllDto.getPageNum()), NumberUtil.parseInt(selectPageAllDto.getPageSize()));
        if(StrUtil.isBlank(selectPageAllDto.getRepState())){
            return DataResult.build9200("repState数据缺失");
        }
        //获取数据
        List<RepairWorkOrder> repairWorkOrderAll = commonService.getAppRepairWorkOrderAll(loginInfo, selectPageAllDto);
        //故障类别
        Map<Integer, String> faultMap = commonService.getDictMap("TYPE_REP_FAULT");
        //紧急程度
        Map<Integer, String> urgencyMap = commonService.getDictMap("TYPE_URGENCY_LEVEL");
        Map<Long, EquipmentTree> equipmentTreeMap = commonService.getEquipmentTreeMap();
        List<Map<String, Object>> list = new ArrayList<>();
        for (RepairWorkOrder rw : repairWorkOrderAll) {
            Map<String, Object> map = new HashMap<>();
            map.put("repId", rw.getRepId());
            map.put("workOrderCode", rw.getWorkOrderCode());
            map.put("emLocation", rw.getEmLocation());
            map.put("employeeName",commonService.getEmployeeNameById(rw.getEmployeeId()));
            map.put("repDesc",rw.getRepDesc());
            map.put("emName",rw.getEmName());
            map.put("repState",rw.getRepState());
            map.put("urgencyLevel",rw.getUrgencyLevel());
            map.put("repFaultType",rw.getRepFaultType());
            map.put("urgencyLevelName",urgencyMap.getOrDefault(rw.getUrgencyLevel(),""));
            map.put("repFaultTypeName",faultMap.getOrDefault(rw.getRepFaultType(),""));
            map.put("createTime", DateUtil.format(rw.getCreateTime(), "yyyy-MM-dd HH:mm:ss"));
            map.put("treeName", commonService.getTreeStrById(equipmentTreeMap,rw.getTreeId()));
            list.add(map);
        }
        //分页总数
        GridData gridData = new GridData(pageObject.getTotal(),list);
        return DataResult.build9100(gridData);
    }


}
